#!/bin/bash

# Slackware build script for fcitx-mozc

# Copyright 2015-2023 Kuro_CODE25 JAPAN
# All rights reserved.
#
# Licensed under the MIT License.
# See https://opensource.org/licenses/MIT for details.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=fcitx-mozc
VERSION=${VERSION:-2.26.4360.102.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

ZIPCODE_REL=202011
PROTOBUF_REV=fde7cf7358ec7cd69e8db9be4f1fa6a5c431386a
GYP_REV=caa60026e223fc501e8b337fd5086ece4028b1c6
JAPANESE_USAGE_DICT_REV=e5b3425575734c323e1d947009dd74709437b684
ABSEIL_CPP_REV=0f3bb466b868b523cf1dc9b2aaaed65c77b28862
BREAKPAD_REV=216cea7bca53fa441a3ee0d0f5fd339a3a894224
GOOGLETEST_REV=703bd9caab50b139428cea1aaff9974ebee5742e
JSONCPP_REV=11086dd6a7eba04289944367ca82cea71299ed70
MOZC_REV=ca82d39d08df3359fdf5c653a1f56b3e95cbe2ce
unzip ${CWD}/x-ken-all-${ZIPCODE_REL}.zip
unzip ${CWD}/jigyosyo-${ZIPCODE_REL}.zip
unzip ${CWD}/mozc-$MOZC_REV.zip
unzip ${CWD}/japanese-usage-dictionary-$JAPANESE_USAGE_DICT_REV.zip
unzip ${CWD}/gyp-$GYP_REV.zip
unzip ${CWD}/protobuf-$PROTOBUF_REV.zip
unzip ${CWD}/googletest-$GOOGLETEST_REV.zip
unzip ${CWD}/jsoncpp-$JSONCPP_REV.zip
unzip ${CWD}/breakpad-$BREAKPAD_REV.zip
unzip ${CWD}/abseil-cpp-$ABSEIL_CPP_REV.zip
mv mozc-$MOZC_REV mozc
mv japanese-usage-dictionary-$JAPANESE_USAGE_DICT_REV japanese_usage_dictionary
mv protobuf-$PROTOBUF_REV protobuf
mv gyp-$GYP_REV gyp
mv googletest-$GOOGLETEST_REV googletest
mv jsoncpp-$JSONCPP_REV jsoncpp
mv breakpad-$BREAKPAD_REV breakpad
mv abseil-cpp-$ABSEIL_CPP_REV abseil-cpp

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

CFLAGS="${CFLAGS} -fvisibility=hidden"
CXXFLAGS="${CXXFLAGS} -fvisibility=hidden"

cd $TMP/$PRGNAM-$VERSION/mozc/src
rm unix/fcitx5/fcitx5.gyp
PYTHONPATH="$PWD:$PYTHONPATH" python3 dictionary/gen_zip_code_seed.py \
  --zip_code="$TMP/$PRGNAM-$VERSION/x-ken-all.csv" \
  --jigyosyo="$TMP/$PRGNAM-$VERSION/JIGYOSYO.CSV" >> data/dictionary_oss/dictionary09.txt

cd $TMP/$PRGNAM-$VERSION
for dep in gyp protobuf japanese_usage_dictionary googletest jsoncpp breakpad abseil-cpp
do
  cp -a $dep mozc/src/third_party/
done

cd $TMP/$PRGNAM-$VERSION/mozc/src
QTDIR=/usr GYP_DEFINES="document_dir=/usr/share/licenses/$PRGNAM use_libzinnia=1" python3 build_mozc.py gyp
sed -i 's|python|python3|' ../scripts/build
TARGETS="server/server.gyp:mozc_server gui/gui.gyp:mozc_tool unix/fcitx/fcitx.gyp:fcitx-mozc" \
  _bldtype=Release ../scripts/build

PREFIX=${PKG}/usr
_bldtype=Release

install -D -m 755 "out_linux/${_bldtype}/mozc_server" "${PREFIX}/lib${LIBDIRSUFFIX}/mozc/mozc_server"
install -D -m 755 "out_linux/${_bldtype}/mozc_tool"   "${PREFIX}/lib${LIBDIRSUFFIX}/mozc/mozc_tool"
install -d "${PREFIX}/share/doc/mozc/"
install -m 644 data/installer/*.html "${PREFIX}/share/doc/mozc/"

for mofile in out_linux/${_bldtype}/gen/unix/fcitx/po/*.mo
do
    filename=`basename $mofile`
    lang=${filename/.mo/}
    install -D -m 644 "$mofile" "${PREFIX}/share/locale/$lang/LC_MESSAGES/fcitx-mozc.mo"
done

install -D -m 755 "out_linux/${_bldtype}/fcitx-mozc.so" "${PREFIX}/lib${LIBDIRSUFFIX}/fcitx/fcitx-mozc.so"
install -D -m 644 unix/fcitx/fcitx-mozc.conf "${PREFIX}/share/fcitx/addon/fcitx-mozc.conf"
install -D -m 644 unix/fcitx/mozc.conf "${PREFIX}/share/fcitx/inputmethod/mozc.conf"

install -d "${PREFIX}/share/fcitx/mozc/icon"
install -D -m 644 data/images/product_icon_32bpp-128.png "${PREFIX}/share/fcitx/mozc/icon/mozc.png"
install -D -m 644 ../scripts/icons/ui-alpha_full.png "${PREFIX}/share/fcitx/mozc/icon/mozc-alpha_full.png"
install -D -m 644 ../scripts/icons/ui-alpha_half.png "${PREFIX}/share/fcitx/mozc/icon/mozc-alpha_half.png"
install -D -m 644 ../scripts/icons/ui-direct.png "${PREFIX}/share/fcitx/mozc/icon/mozc-direct.png"
install -D -m 644 ../scripts/icons/ui-hiragana.png "${PREFIX}/share/fcitx/mozc/icon/mozc-hiragana.png"
install -D -m 644 ../scripts/icons/ui-katakana_full.png "${PREFIX}/share/fcitx/mozc/icon/mozc-katakana_full.png"
install -D -m 644 ../scripts/icons/ui-katakana_half.png "${PREFIX}/share/fcitx/mozc/icon/mozc-katakana_half.png"
install -D -m 644 ../scripts/icons/ui-dictionary.png "${PREFIX}/share/fcitx/mozc/icon/mozc-dictionary.png"
install -D -m 644 ../scripts/icons/ui-properties.png "${PREFIX}/share/fcitx/mozc/icon/mozc-properties.png"
install -D -m 644 ../scripts/icons/ui-tool.png "${PREFIX}/share/fcitx/mozc/icon/mozc-tool.png"

rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

cd $TMP/$PRGNAM-$VERSION/mozc
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  AUTHORS CONTRIBUTORS LICENSE README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
